package com.rain.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.rain.activity.adapter.Myadapter;
import com.rain.activity.adapter.QuestionAdapter;
import com.rain.activity.utils.BaseActivity;
import com.rain.activity.utils.Globals;
import com.rain.activity.utils.QuestionDAO;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MainActivity extends BaseActivity {
    private ViewPager pager;
    private List<View> allViews;
    private Myadapter myadapter;
    private String [] topTitles = new String[]{"程序员面试宝典","全部问题","搜索问题"};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.init("程序员面试宝典", 0);
        pager = (ViewPager) findViewById(R.id.mainPage);
        allViews = new ArrayList<>();
        View mainView = LayoutInflater.from(this).inflate(R.layout.page_main,null);
        View questionView = LayoutInflater.from(this).inflate(R.layout.page_question,null);
        View searchView = LayoutInflater.from(this).inflate(R.layout.page_search,null);
        allViews.add(mainView);
        allViews.add(questionView);
        allViews.add(searchView);
        initQuestionView(questionView);
        initSearch(searchView);
        myadapter = new Myadapter(allViews);
        pager.setAdapter(myadapter);
        pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                topTitle.setText(topTitles[position]);
                for(int i =0;i<btns.length;i++){
                    if(i==position){
                        btns[i].setBackgroundResource(btnImgsSelect[i]);
                    }
                    else{
                        btns[i].setBackgroundResource(btnImgs[i]);
                    }
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private int i;
    private int [] btnImgsSelect = new int[]{R.drawable.bt01_b,R.drawable.bt02_b,R.drawable.bt03_b};
    private int [] btnImgs = new int[]{R.drawable.bt01_a,R.drawable.bt02_a,R.drawable.bt03_a};
    private Button[] btns = new Button[3];
    private TextView topTitle;
    private Button setting ;
    private PopupWindow window;
    public void init(String title,int index){
        if(topTitle==null){  topTitle = (TextView) findViewById(R.id.toptitle);}
        else
            topTitle.setText(title);
        btns[0]= (Button) findViewById(R.id.bt1);
        btns[1]= (Button) findViewById(R.id.bt2);
        btns[2]= (Button) findViewById(R.id.bt3);
        for(i =0;i<btns.length;i++){
            final int finalI = i;
            if(i==index){
                btns[i].setBackgroundResource(btnImgsSelect[i]);
            }
            else{
                btns[i].setBackgroundResource(btnImgs[i]);
            }
            btns[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    pager.setCurrentItem(finalI,true);
                }
            });
        }
        setting = (Button) findViewById(R.id.setting);
        setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                window = new PopupWindow(Globals.SCREEN_WIDTH/4,Globals.SCREEN_HEIGHT/6);
                View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.win_list,null);
                window.setContentView(view);
                if(window.isShowing()){
                    window.dismiss();
                }
                else{
                    window.showAsDropDown(setting);
                }
                TextView win_version = (TextView) view.findViewById(R.id.win_version);
                TextView win_about = (TextView) view.findViewById(R.id.win_about);
                TextView win_exit = (TextView) view.findViewById(R.id.win_exit);
                win_version.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        window.dismiss();
                        Toast.makeText(MainActivity.this, "1.1版本", Toast.LENGTH_LONG).show();
                    }
                });
                win_about.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        window.dismiss();
                        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                        builder.setTitle("关于我们");
                        builder.setMessage("北航大数据");
                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        });
                        builder.create().show();
                    }
                });
                win_exit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        window.dismiss();
                        finish();
                    }
                });
            }
        });
    }
    private ListView list;
    private List<Map<String,Object>> values;
    private QuestionAdapter adapter;
    //分页显示
    private int pageSize=16;
    private int pageNum =1;
    private int recordCount;
    private int pageCount;

    private int first;
    private  int visible;
    private int total;
    private  TextView footView;
    private String keyword;
    private  void initQuestionView(View questionView){
         list = (ListView)questionView.findViewById(R.id.list);
        if (keyword==null){
            keyword = "";
        }
        values= QuestionDAO.loadDataPage(pageNum, pageSize, keyword);
        recordCount = QuestionDAO.getPageCount(keyword);
        pageCount = (recordCount-1)/pageSize+1;
        footView = new TextView(this);
        footView.setText("正在加载，请稍后。。。");
        footView.setTextColor(Color.BLACK);
        footView.setTextSize(14);
        list.addFooterView(footView);
        adapter = new QuestionAdapter(this,values);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                LinearLayout layout = (LinearLayout) view;
                Map<String, Object> map = values.get(position);
                if (layout.getChildCount() == 1) {
                    TextView tv = new TextView(MainActivity.this);
                    tv.setText(map.get("ans").toString());
                    tv.setTextSize(14);
                    tv.setTextColor(Color.RED);
                    layout.addView(tv);
                    map.put("showflag",true);
                } else {
                    layout.removeViewAt(1);
                    map.put("showflag", false);
                }
            }
        });
        list.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if(scrollState == SCROLL_STATE_IDLE && first + visible == total&& total != 0){
                    if(pageNum<pageCount){
                        pageNum++;
                        values.addAll(QuestionDAO.loadDataPage(pageNum,pageSize,keyword));
                        adapter.notifyDataSetChanged();
                    }
                    else{
                        if(list.getFooterViewsCount()>0)
                            list.removeFooterView(footView);
                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                first = firstVisibleItem;
                visible = visibleItemCount;
                total = totalItemCount;
            }
        });
    }
    private Button searchBtn ;
    private EditText editText;
    private  void initSearch(View searchView){
        editText = (EditText)searchView.findViewById(R.id.input);
        searchBtn = (Button)searchView.findViewById(R.id.search_btn);
        searchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    values.clear();
                    keyword = editText.getText().toString();
                    pageNum = 1;
                    values.addAll(QuestionDAO.loadDataPage(pageNum,pageSize,keyword));
                    adapter.notifyDataSetChanged();
                    pager.setCurrentItem(1,true);
            }
        });
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
